<?php
/**
 * 异步交互控制器
 * @author jianhui
 *
 */
class Ajax extends MY_Controller{
    function Ajax(){
        parent::MY_Controller();
    }
    /**
     * 载入年份列表
     */
    function loadYearList(){
        //时间单位配置
        $timeUnitList = $this->timeUnitModel->getCacheData('yearList');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($timeUnitList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入时段列表
     */
    function loadTimeUnitList(){
        $year = $this->input->post('year');
        $year || $year = $this->config->item('nowYear');
        //时间单位配置
        $timeUnitList = $this->timeUnitModel->getTimeUnitList($year);
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($timeUnitList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入广宣传播广宣形式
     */
    function loadOnlineMediaType(){
        $this->load->model('carLevelModel');
        $carLevelActiveList = $this->carLevelModel->getCacheData('carLevelActiveList');
        $year = (int)$this->input->post('year');
        $timeUnit = (int)$this->input->post('timeUnit');
        $eventType = isset($carLevelActiveList[3][$year][$timeUnit]) ? $carLevelActiveList[3][$year][$timeUnit] : array();
        
        //$eventType = $this->config->item('onlineMediaType');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if(isset($eventType)){
            foreach ($eventType as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入营销活动-活动类型
     */
    function loadOfflineMediaType(){
        $mediaTypeList = $this->config->item('offlineMediaType');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if(isset($mediaTypeList)){
            foreach ($mediaTypeList as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入营销活动-活动形式
     */
    function loadOfflineMediaForm(){
        $mediaType = (int)$this->input->post('mediaType');
        $mediaFormList = $this->config->item('offlineMediaForm');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if(isset($mediaFormList[$mediaType])){
            foreach ($mediaFormList[$mediaType] as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入地区列表
     */
    function loadPlaceList(){
        $type = $this->input->post('type');
        $lastLevelValue = $this->input->post('lastLevelValue');
        $list = array();
        switch ($type){
            case 'area':
                $list = $this->userLib->getAreaList();
                array_unshift($list, array('key'=>'', 'value'=>'请选择地区'));
                break;
            case 'region':
                $lastLevelValue && $list = $this->userLib->getRegionList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择大区'));
                break;
            case 'spsarea':
                $lastLevelValue && $list = $this->userLib->getSpsareaList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择小区'));
                break;
            case 'province':
                $lastLevelValue && $list = $this->userLib->getProvinceList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择省份'));
                break;
            case 'city':
                $lastLevelValue && $list = $this->userLib->getCityList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择城市'));
                break;
            case 'store':
                $lastLevelValue && $list = $this->userLib->getStoreList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择专营店'));
                break;
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 搜索专营店列表
     */
    function searchStore(){
        //专营店筛选
        $area = $this->input->post('area');
        $region = $this->input->post('region');
        $spsarea = $this->input->post('spsarea');
        $province = $this->input->post('province');
        $city = $this->input->post('city');
        $keyword = $this->input->post('keyword');
        $fundType = (int)$this->input->post('fundType');
        //权限内专营店列表
        $storeId = 0;
        $viewStoreIds = $this->userLib->getViewStoreIds();
        $viewStoreIds != '*' && $storeId = $viewStoreIds;
        //如果为城市联合基金类型，则只搜索主单店
        if ($fundType == 2){
            $this->load->model('jurSellpointModel');
            $baichengIds = $this->jurSellpointModel->get100CityStoreIds();
            if ($storeId){
                $storeId = array_intersect((array)$storeId, (array)$baichengIds);
            }else{
                $storeId = $baichengIds;
            }
        }
        //查找对应地区列表
        $this->load->model('sellpointModel');
        $searchStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea, $province, $city, $storeId, $keyword);
        $list = array();
        foreach($searchStoreList as $v){
            $list[] = array('key'=>(int)$v['userId'], 'value'=>$v['storeName']);
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 载入车展等级
     */
    function loadCarLevel(){
        //$carshowLevel = $this->config->item('carshowLevel');
        $type = (int)$this->input->post('type');
        $year = (int)$this->input->post('year');
        $timeUnit = (int)$this->input->post('timeUnit');
        $this->load->model('carLevelModel');
        $carLevelList = $this->carLevelModel->getCacheData('carLevelActiveList');
        $carshowLevel = isset($carLevelList[$type][$year][$timeUnit]) ? $carLevelList[$type][$year][$timeUnit] : array();
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if(isset($carshowLevel)){
            foreach ($carshowLevel as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        echo jsonEncode($list);
        exit();
    }
}